package sctu.edu.java.year2016.team02.huangguojing.day20160605.Chapter12_2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Eoly on 2016/6/5.
 * 编写程序，使用字符输入、输出流读取文件，将一段文字加密后存入文件，然后再读取，并将加密前与加密后的文件输出。
 */
public class Windows extends JFrame implements ActionListener {
    JTextArea TextOne=new JTextArea();
    JTextArea TextTwo=new JTextArea();
    JLabel key = new JLabel("Key-words:");
    JLabel chooseOne = new JLabel("Before:");
    JLabel chooseTwo = new JLabel("After:");
    Button open = new Button("Open");
    Button save = new Button("Save");
    Button Change = new Button("Change");
    Button SaveAs = new Button("Save as...");
    JFileChooser openLift = new JFileChooser("D:/dev");
    JTextField Keys=new JTextField();
    JFileChooser Save = new JFileChooser("D:/dev");
    JLabel FilePath = new JLabel();
    JLabel messege = new JLabel();
    public Windows(String title){
        super(title);
        WindowsInit();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(null);
        setResizable(false);
        add(TextOne);
        add(TextTwo);
        add(Keys);
        add(key);
        add(chooseOne);
        add(chooseTwo);
        add(open);
        add(save);
        add(SaveAs);
        add(Change);
        add(FilePath);
        add(messege);
        setSize(1000, 400);
        setVisible(true);
    }
    private void WindowsInit(){
        Keys.setBounds(20,40,200,25);
        key.setBounds(20,15,200,25);
        TextOne.setBounds(20,100,400,250);
        TextTwo.setBounds(580,100,400,250);
        chooseOne.setBounds(20,75,400,25);
        chooseTwo.setBounds(580,75,400,25);
        messege.setBounds(250,40,200,25);
        FilePath.setBounds(580,15,400,25);
        save.setBounds(580,50,75,25);
        open.setBounds(457,150,75,25);
        Change.setBounds(457, 200, 75, 25);
        SaveAs.setBounds(680, 50, 75, 25);
        TextOne.setLineWrap(true);
        TextTwo.setLineWrap(true);
        save.setEnabled(false);
        SaveAs.setEnabled(false);
        TextTwo.setEditable(false);
        FilePath.setEnabled(false);
        open.addActionListener(this);
        save.addActionListener(this);
        SaveAs.addActionListener(this);
        Change.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == Change) {
            KeysWords text = new KeysWords();
            text.setPreWords(TextOne.getText());
            text.Changes(Keys.getText());
            TextTwo.setText(text.getEndWords());
            SaveAs.setEnabled(true);
            messege.setText("Changing successfully!");
        }else if (e.getSource()==save){
            fileOperate file = new fileOperate(FilePath.getText());
            file.outPut(TextTwo.getText());
            messege.setText("Saving successfully!");
        }else if (e.getSource()==open){
            openLift.showOpenDialog(this);
            if(openLift.getSelectedFile()!=null) {
                fileOperate file = new fileOperate(openLift.getSelectedFile());
                TextOne.setText(file.inPut());
                messege.setText("Opening successfully!");
            }
        }else if (e.getSource()==SaveAs){
            Save.showSaveDialog(this);
            if(Save.getSelectedFile()!=null) {
                fileOperate file = new fileOperate(Save.getSelectedFile());
                file.outPut(TextTwo.getText());
                messege.setText("Writing successfully!");
                FilePath.setText(file.file.getAbsolutePath());
                save.setEnabled(true);
            }
        }
    }
}
